package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.PackageService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class PackageController {
    @Autowired
    private PackageService packageService;


    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     * @throws
     * */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updatePackage(@RequestBody SetmealDTO setmealDTO){
        packageService.updatePackage(setmealDTO);
        return Result.success();
    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     * */
    @GetMapping("/page")
    public Result Page(SetmealPageQueryDTO  setmealPageQueryDTO){
        PageResult pageResult = packageService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 添加套餐
     * @param setmealDTO
     * */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addPackage(@RequestBody SetmealDTO setmealDTO){
        packageService.addPackage(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售停售
     * @param status
     * */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result status(@PathVariable String status,
                         Long id) throws Exception {
        packageService.status(status,id);
        return Result.success();
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     * */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deletePackage(@RequestParam List<Integer> ids)
            throws Exception {
        packageService.deletePackage(ids);
        return Result.success();
    }


    /**
     * 根据id查询套餐
     * @param id
     * */
    @GetMapping("/{id}")
    public Result getPackageById(@PathVariable Long id){
        SetmealVO setmealVO = packageService.getPackageById(id);
        return Result.success(setmealVO);
    }


}
